#!/bin/bash

cachesave_recurse()
{
	target="$1"
	printf "Doing $target\n"
	newtarget="${target//eatmydata/cachesave}"

	if [[ $target = *eatmydata* ]]; then
		# replace all matches
		echo mv "$target" "$newtarget"
		mv "$target" "$newtarget"
	fi

	target="$newtarget"

	if [[ -d $target ]]; then
		for entry in "$target"/*; do
			cachesave_recurse "$entry"
		done
	fi

}

for f in $(grep -R eatmydata -l); do sed -i -e "s/eatmydata/cachesave/g" $f; done

cachesave_recurse .

printf "\n\nAll done, just copy the libcachesave-VERSION.c over existing libcachesave/libcachesave.c\n"
